#!/usr/bin/perl -w
use strict;
use Getopt::Long;

#88888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Usage

#88888888888888888888888888888888888888888888888888888888888888888888888888888888

my $usage = <<'USAGE';

USAGE:

    tree_to_slr_format.pl
                --sequence seq_file
                < tree.input
				> tree.output

    Converts tree names to their positions in the sequence file

USAGE


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Get options

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# mandatory

# optional parameters
my $sequence = '';
my $help;
GetOptions(
			'sequence=s'			=> \$sequence,
			'help'				=> \$help);


die $usage if ($help);
die $usage unless ($sequence);

open SEQ, $sequence or die "Error:\n\tCould not open $sequence\n";

my %convert;
my $cnt_seq = 1;
{
	#ignore line1
	my $line1 = <SEQ>;
	for (<SEQ>)
	{
		my ($name) = split /\s+/;
		next unless $name;
		$convert{$name} = $cnt_seq++;
	}
}

print $cnt_seq - 1, " 1\n";
my @keys = reverse sort keys %convert;
my @tree_lines = <>;
chomp @tree_lines;
my $tree_lines = join("", @tree_lines);
$tree_lines =~ s/\[.*?\]//g;
for my $a(@keys)
{
	$tree_lines =~ s/$a/$convert{$a}/g;
}
print $tree_lines;
